package com.wlj.web.filter;

import com.wlj.web.request.WljHttpServletRequestWrapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.filter.OncePerRequestFilter;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;


//@Component//无需添加此注解，当在启动类添加@ServletComponentScan注解后，会自动将带有@WebFilter的注解进行注入
//使用@ServletComponentScan注解后，Servlet、Filter、Listener可以直接通过@WebServlet、@WebFilter、@WebListener注解自动注册，无需其他代码
@WebFilter(filterName = "RequestReplaceFilter", urlPatterns = { "/*" })
public class RequestReplaceFilter extends OncePerRequestFilter {
    private static final Logger logger = LoggerFactory.getLogger(RequestReplaceFilter.class);

    @Override
    protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
        if (!(request instanceof WljHttpServletRequestWrapper)) {
            request = new WljHttpServletRequestWrapper(request);
        }
        filterChain.doFilter(request, response);
    }
}
